//
//  Utilities.m
//  EasyLessons
//
//  Created by 易 on 2017/6/25.
//  Copyright © 2017年 蜂窝. All rights reserved.
//

#import "Utilities.h"

@implementation Utilities

+ (void)underLineSlideAnimation:(UIView *)underlineView withAlignView:(UIView *)alignView  andAnimationView:(UIView *)animationView {
    if (underlineView.center.x > alignView.center.x) {
        animationView.alpha = 0;
        animationView.frame = CGRectMake(-SCREEN_WIDTH, 102, SCREEN_WIDTH, SCREEN_HEIGHT - 102);
        [UIView animateWithDuration:0.3 animations:^{
            animationView.alpha = 1;
            animationView.frame = CGRectMake(0, 102, SCREEN_WIDTH, SCREEN_HEIGHT - 102);
        } completion:nil];
    } else if (underlineView.center.x < alignView.center.x) {
        animationView.alpha = 0;
        animationView.frame = CGRectMake(SCREEN_WIDTH * 2, 102, SCREEN_HEIGHT, SCREEN_HEIGHT - 102);
        [UIView animateWithDuration:0.3 animations:^{
            animationView.alpha = 1;
            animationView.frame = CGRectMake(0, 102, SCREEN_WIDTH, SCREEN_HEIGHT - 102);
        } completion:nil];
    }
    [UIView animateWithDuration:0.3 animations:^{
        underlineView.center = CGPointMake(alignView.center.x, alignView.frame.size.height - underlineView.frame.size.height / 2);
    } completion:nil];
}

// 计算label 高宽
+ (CGRect)calculateWidthAndHeightWithWidth:(CGFloat)width height:(CGFloat)height text:(NSString *)text font:(UIFont *)font
{
    CGRect titleSize = [text boundingRectWithSize:CGSizeMake(width, height) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  | NSStringDrawingUsesLineFragmentOrigin            attributes:@{NSFontAttributeName: font} context:nil];
    return titleSize;
}

//给UILabel设置行间距和字间距
+ (void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = 3; //设置行间距
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    //设置字间距 NSKernAttributeName:@1.5f
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.2f};
    
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    label.attributedText = attributeStr;
}

@end
